# Set the prefix from C-b to C-a
unbind C-b # send-prefix
set -g prefix C-a
bind C-a send-prefix

# Set the delay between prefix and command
set -sg escape-time 0

# Set the base index for windows and panes to 1 instead of 0
set -g base-index 1
set -wg pane-base-index 1

# Reload the file with prefix-r
unbind S-Up    # refresh-client -U 10
unbind S-Down  # refresh-client -D 10
unbind S-Left  # refresh-client -L 10
unbind S-Right # refresh-client -R 10
unbind r # refresh-client
bind r source-file ~/.config/tmux/tmux.conf \; display "tmux.conf reloaded!"

# Use vi mode for working with buffer
set -wg mode-keys vi

# vi copy mode: pbcopy is installed on macOS by default, on Linux xclip needs to be installed
bind-key -T copy-mode-vi v send -X begin-selection
if-shell "uname | grep -q Darwin" "bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'pbcopy'"
if-shell "uname | grep -q Darwin" "bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel 'pbcopy'"
if-shell "uname | grep -q Linux" "bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'xclip -in -filter -selection primary | xclip -in -selection clipboard > /dev/null'"
if-shell "uname | grep -q Linux" "bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel 'xclip -in -filter -selection primary | xclip -in -selection clipboard > /dev/null'"

# choose-tree with the pane zoomed with prefix-w
unbind s # choose-tree -Zs
unbind w # choose-tree -Zw
bind w choose-tree -Z

# Split panes with prefix-v, s
unbind \" # split-window
unbind \% # split-window -v
bind v split-window -h -c "#{pane_current_path}"
bind s split-window -v -c "#{pane_current_path}"

# Move between panes with prefix-H, J, K, L
unbind L # switch-client -l
bind K select-pane -U
bind J select-pane -D
bind H select-pane -L
bind L select-pane -R

# Resize pane with prefix-h, j, k, l
unbind C-Up    # resize-pane -U
unbind C-Down  # resize-pane -D
unbind C-Left  # resize-pane -L
unbind C-Right # resize-pane -R
unbind M-Up    # resize-pane -U 5
unbind M-Down  # resize-pane -D 5
unbind M-Left  # resize-pane -L 5
unbind M-Right # resize-pane -R 5
bind -r k resize-pane -U
bind -r j resize-pane -D
bind -r h resize-pane -L
bind -r l resize-pane -R

# Mouse support
set -g mouse on

# Set the default terminal terminfo
set -g default-terminal "tmux-256color"

# Update the status line every sixty seconds
set -g status-interval 60

# Message settings
set -g message-style fg=white,bold

# Style
set -g status off
set -wg pane-border-status top
set -wg pane-border-format '#[align=centre] #{?#{&&:#{pane_active},#{client_prefix}},#[fg=red],}\
#{pane_current_command}:#{pane_tty} #{?pane_active,#S@#H:W#I/#{session_windows} ,}\
P#P#{?pane_active,/#{window_panes},} '
set -wg pane-active-border-style fg=white,bold
set -wg pane-border-style fg=magenta
set -wg pane-border-lines heavy
set -wg popup-border-lines heavy

# Update $DISPLAY and/or Vim
set-hook -g client-attached 'run-shell ~/.config/tmux/update_display_vim.sh'
set-hook -g client-session-changed 'run-shell ~/.config/tmux/update_vim.sh'
set -g focus-events on
set-hook -g pane-focus-in 'run-shell ~/.config/tmux/update_vim.sh'

# True colour support
set -as terminal-overrides ",xterm*:Tc"

# Set and reset the cursor style
set -ag terminal-overrides ',*:Ss=\E[%p1%d q:Se=\E[5 q'

# Italics, needed by iTerm2
set -as terminal-overrides ',*:sitm=\E[3m'

# Curly underline, needed by kitty and iTerm2
set -as terminal-overrides ',*:Smulx=\E[4::%p1%dm'

# Coloured underline
set -as terminal-overrides ',*:Setulc=\E[58::2::%p1%{65536}%/%d::%p1%{256}%/%{255}%&%d::%p1%{255}%&%d%;m'
